package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.OrderDao;
import com.qst.crop.entity.Order;
import com.qst.crop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl  implements OrderService {
    @Autowired
    private OrderDao orderDao;

    private Integer pageSize=30;

    @Override
    public PageInfo<Order> selectAll(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList=orderDao.selectAll();
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> selectByExamble(String key,Integer pageNum) {
        //有条件的查询    type类型 你可以直接给一个string  最好的方式是用实体类作为参数进去查询
        Order order = new Order();

        order.setType("goods");

        if (key!=null&&!key.equals("")){
            order.setContent(key);
        }
        log.info(order.getContent());
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList=orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        return orderPageInfo;
    }



    @Override
    public PageInfo<Order> selectGoodsByKeys(String keys, Integer pageNum, String name) {
        //创建Order实例
        Order order = new Order();
        order.setType("goods");
        order.setContent(keys);

        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println("========="+principal);
            Iterator it = principal.getAuthorities().iterator(); // 获得一个迭代子
            while(it.hasNext()) {
                Object obj = it.next(); // 得到下一个元素
                String role = obj.toString();
                if(!role.equals("admin"))
                {
                    order.setOwnName(name);
                    order.setOrderStatus(0);
                    break;
                }
            }
        }catch (Exception e){

        }

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Order> orders = orderDao.selectByKeys(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }

    @Override
    public Order selectById(Integer id) {
        Order order = orderDao.selectByPrimaryKey(id);
        return order;
    }

    @Override
    public PageInfo<Order> selectByType(String type, Integer pageNum) {
        //创建Order实例
        Order order = new Order();
        order.setType(type);

        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = principal.getUsername();
            log.info("当前的用户是:");
            log.info(username);
            System.out.println("========="+principal);
            Iterator it = principal.getAuthorities().iterator(); // 获得一个迭代子
            while(it.hasNext()) {
                Object obj = it.next(); // 得到下一个元素
                String role = obj.toString();
                if(!role.equals("admin"))
                {
                    order.setOwnName(username);
                    order.setOrderStatus(0);
                    break;
                }
            }
        }catch (Exception e){

        }

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Order> orders = orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }

    @Override
    public void add(Order order) {
        orderDao.insertSelective(order);
    }

    @Override
    public void update(Order order) {
        orderDao.updateByPrimaryKeySelective(order);
    }
    @Override
    public void delete(Integer id) {
          orderDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Order> selectAllNeeds(Integer pageNum) {
        //创建Order实例
        Order order = new Order();
        order.setType("needs");

        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = principal.getUsername();
            log.info("当前的用户是:");
            log.info(username);
            System.out.println("========="+principal);
            Iterator it = principal.getAuthorities().iterator(); // 获得一个迭代子
            while(it.hasNext()) {
                Object obj = it.next(); // 得到下一个元素
                String role = obj.toString();
                if(!role.equals("admin"))
                {
                    break;
                }
            }
        }catch (Exception e){

        }

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Order> orders = orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> selectNeedsByKeys(String keys, Integer pageNum, String name) {
        //创建Order实例
        Order order = new Order();
        order.setType("needs");
        order.setContent(keys);

        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = principal.getUsername();
            log.info("当前的用户是:");
            log.info(username);
            System.out.println("========="+principal);
            Iterator it = principal.getAuthorities().iterator(); // 获得一个迭代子
            while(it.hasNext()) {
                Object obj = it.next(); // 得到下一个元素
                String role = obj.toString();
                if(!role.equals("admin"))
                {
                    break;
                }
            }
        }catch (Exception e){

        }

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Order> orders = orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> searchMyNeedsByKeys(String keys, Integer pageNum, String name) {
        //创建Order实例
        Order order = new Order();
        order.setType("needs");
        order.setContent(keys);

        try {
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = principal.getUsername();
            log.info("当前的用户是:");
            log.info(username);
            System.out.println("========="+principal);
            Iterator it = principal.getAuthorities().iterator(); // 获得一个迭代子
            while(it.hasNext()) {
                Object obj = it.next(); // 得到下一个元素
                String role = obj.toString();
                if(!role.equals("admin"))
                {
                    order.setOrderStatus(0);
                    order.setOwnName(name);
                    break;
                }
            }
        }catch (Exception e){

        }

        //分页
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Order> orders = orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }
}
